import buffer from '@ohos.buffer';
import * as structureUtil from "../structureUtil";
import Header, { headerSize, headerStructure } from "./Header";

export default class Packet {
  static parse(buf: buffer.Buffer) {
    const { header, bodyBuffer } = Packet.parseHeader(buf);
    let offset = 0;
    const wordCount = bodyBuffer.readInt8(offset);
    offset += 1;
    const dataCount = bodyBuffer.readUInt16LE(offset);
    offset += 2;
    const dataBuffer = bodyBuffer.subarray(offset);
    const dialects = Packet.parseList(dataBuffer) as string[]; // TODO: export into Negotiate packet
    const body: any = { dialects };
    return { header, body };
  }

  static parseHeader(buf: buffer.Buffer) {
    const header = structureUtil.parseStructure(buf, headerStructure) as Header;
    const bodyBuffer = buf.subarray(headerSize);
    return { header, bodyBuffer };
  }

  static parseList(buf: buffer.Buffer) {
    let offset = 0;
    let currentBytes: number[] = [];
    let terminated = true;
    let format = 0;
    const list = [];
    while (offset < buf.length) {
      const byte = buf.readInt8(offset);
      if (terminated) {
        format = byte;
        offset++;
        terminated = false;
        continue;
      }
      if (byte === 0) {
        if (format === 2) {
          const text = buffer.from(currentBytes).toString("ascii");
          list.push(text);
        }
        currentBytes = [];
        terminated = true;
      } else
        currentBytes.push(byte);
      offset++;
    }
    return list;
  }
}